_package("caf.core");

/**
 * 手机服务类
 */
_class("MobileService", "", function(){
	this._init = function(){
		_super._init.call(this);
	};
	this.dispose = function(){
		_super.dispose.apply(this);
	};
	/*
		CloudApi.js

		CloudAPI.Telephony
			void sendCall(<String>phoneNum)
		CloudAPI.Messaging
			void sendSms(<String>phoneNum, <String>msg)
		CloudAPI.PIM
			Boolean getTYToken(<String> onSuccessHandler)
			function getTokenSuccess(token){
				alert("token: " + token);
			}
		CloudAPI.Multimedia
			String takePhoto(<String> onSuccessHandler)
		CloudAPI.Device
			void launchApplication(<String> url)
			void scanBarCode(<String> onSuccessHandler)
		CloudAPI.Device.DeviceStateInfo
			String requestLocationInfo(<String> onLocationChanged)
		CloudAPI.Android.Telephony
			void startCall(<String> phoneNum)
		CloudAPI.Android.Messaging
			void startSms(<String>phoneNum, <String>msg)

		mobile.invoke("Telephony.sendCall", phoneNum);                             //Telephony void sendCall(String phoneNum)
		mobile.invoke("Messaging.sendSms", phoneNum, msg);                         //Messaging void sendSms(String phoneNum, String msg)
		mobile.invoke("PIM.sendSms", phoneNum, msg);                               //PIM Boolean getTYToken(String onSuccessHandler)
		mobile.invoke("Multimedia.takePhoto", onSuccessHandler);                   //Multimedia String takePhoto(String onSuccessHandler)
		mobile.invoke("Device.launchApplication", url);                            //Device void launchApplication(String url)
		mobile.invoke("Device.scanBarCode", onSuccessHandler);                     //Device void scanBarCode(String onSuccessHandler)
		mobile.invoke("Device.StateInfo.requestLocationInfo", onLocationChanged);  //Device.DeviceStateInfo String requestLocationInfo(String onLocationChanged)
		mobile.invoke("Android.Telephony.startCall", phoneNum);                    //Android.Telephony void startCall(String phoneNum)
		mobile.invoke("Android.Messaging.startSms", phoneNum, msg);                //Android.Messaging void startSms(String phoneNum, String msg)
	*/
	var conf = {
		"Telephony.sendCall"          : "Telephony.sendCall",
		"Messaging.sendSms"           : "Messaging.sendSms",
		"PIM.sendSms"                 : "PIM.getTYToken",
		"Multimedia.takePhoto"        : "Multimedia.takePhoto",
		"Device.runApp"               : "Device.launchApplication",
		"Device.scanBarCode"          : "Device.scanBarCode",
		"Device.StateInfo.reqLocation": "Device.DeviceStateInfo.requestLocationInfo",
		"Android.Telephony.startCall" : "Android.Telephony.startCall",
		"Android.Messaging.startSms"  : "Android.Messaging.startSms"
	};
	/**
	 * 请求一个服务功能
	 */
	this.request = function(key){  //(key, ...)
		var args = [];
		for(var i = 1, len = arguments.length; i < len; i++){
			args.push(arguments[i]);
		}
		var arr = conf[key].split(".");
		var m = arr.pop();
		var obj = CloudAPI;
		for(var i = 0, len = arr.length; i < len; i++){
			obj = obj[arr[i]];
		}
		try{
			return obj[m].apply(obj, args);
		}catch(ex){
			runtime.error(ex.message);
			return null;
		}
	};
});